﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathNet.Numerics.LinearAlgebra;
using System.IO;

namespace NatcarSim {
	class Track {
		List<Vector> mPath = new List<Vector>();

		public void Load(string file) {
			mPath.Clear();
			using(FileStream fs = File.OpenRead(file)) {
				StreamReader sr = new StreamReader(fs);
				while(!sr.EndOfStream) {
					string line = sr.ReadLine();
					string[] split = line.Substring(1, line.Length - 2).Split(new char[] { ',' });
					double[] components = new double[3];
					for(int i = 0; i < 2; i++) {
						components[i] = Double.Parse(split[i]);
					}
					mPath.Add(new Vector(components));
				}
			}
		}

		public void Save(string file) {
			using(StreamWriter sw = new StreamWriter(file)) {
				foreach(Vector v in mPath) {
					sw.WriteLine(v.ToString());
				}
				sw.Dispose();
			}
		}

		public void AddPoint(double x, double y) {
			Vector v = new Vector(2);
			v[0] = x;
			v[1] = y;
			mPath.Add(v);
		}

		public void Dump() {
			Console.WriteLine("Dumping track:");
			foreach(Vector p in mPath) {
				Console.WriteLine("{0}, {1}, {2}", p[0], p[1], p[2]);
			}
			Console.WriteLine();
		}

		public void Clear() {
			this.mPath.Clear();
		}

		public List<Vector> Points {
			get { return mPath; }
		}
	}
}
